package base.service.dao;

import com.ibatis.sqlmap.client.SqlMapClient;
import org.apache.log4j.Logger;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.*;

/**
 * <ol>
 * date:11-6-29 editor:yanghongjian
 * <li>创建文档</li>
 * <li>Dao层的基本类</li>
 * <li>通过oracle数据库表名获取列名</li>
 * <li>复制map对象中的值</li>
 * </ol>
 * <ol>
 *
 * @author <a href="mailto:12719889@qq.com">YangHongJian</a>
 * @version 2.0
 * @since 1.6
 */
public class BaseDao {
    private Logger log = Logger.getLogger(this.getClass());
    protected SqlMapClient sqlMap;
    protected List list = new ArrayList();
    protected Map mapParam = new HashMap();

    public BaseDao(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }

    /**
     * 获取当前数据库类型
     *
     * @return 返回当前数据类型名称
     */
    public String getCurDbType() {
        String dbType = "";
        try {
            DatabaseMetaData metaData = sqlMap.getDataSource().getConnection().getMetaData();
            dbType = metaData.getDatabaseProductName();
//            log.info("\n--------------------------------------\n\tcurDb->" + dbType
//                    + "\n\tver->" + metaData.getDatabaseProductVersion()
//                    + "\n\turl->" + metaData.getURL()
//                    + "\n--------------------------------------\n");
        } catch (SQLException e) {
            log.error(e.getMessage());
        }
        return dbType.toLowerCase();
    }

    /**
     * 动态语句查询
     *
     * @param strSql sql字符串
     * @return 返回map集合
     */
    public List getDynamicSql(String strSql) {
//        try {
//            list = sqlMap.queryForList("base.getDynamicSql",strSql);
//        } catch (SQLException e) {
//            log.error(e.getMessage());
//        }
//        return list;
        MapRowHandler mapRow = new MapRowHandler();
        try {
            sqlMap.queryWithRowHandler("base.getDynamicSql",strSql, mapRow);
        } catch (Exception e) {
            String err = "查询出错->出错原因:" + e.getMessage();
            log.error(err, e);
        }
        return mapRow.getMapList();
    }

    /**
     * 通过oracle数据库表名获取列名
     *
     * @param tableName 表名
     * @return 保存列名的map对象
     */
    public Map getTableCols(String tableName) {
        String dbType = getCurDbType();
        String sqlTableCols = "";
        if (dbType.equals("oracle"))
            sqlTableCols = "base.getOracleTableCols";
        else if (dbType.equals("mysql"))
            sqlTableCols = "";

        Map mapCols = new HashMap();

//        try {
//            List list = sqlMap.queryForList(sqlTableCols, tableName);
//            for (Iterator it = list.iterator(); it.hasNext(); )
//                mapCols.put(it.next(), "");
//
//        } catch (SQLException e) {
//            String err = "查询出错->出错原因:" + e.getMessage();
//            log.error(err, e);
//        }
        return mapCols;
    }

    /**
     * 复制map对象中的值
     *
     * @param map    接受第二个参数中map的值
     * @param mapAdd 需要取值的map对象
     */
    protected void mapCopy(Map map, Map mapAdd) {
        if (map == null)
            map = new HashMap();
        if (mapAdd == null)
            return;

        Iterator it = mapAdd.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            map.put(key, mapAdd.get(key) != null ? mapAdd.get(key) : "");
        }
    }
}
